package cn.kgc.javaweb.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

/**
 * @author: mosin
 * @version: v1.0  2022/6/22
 */
public class BaseServlet extends HttpServlet {


    //     /student?method=add
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 获取method的值  方法名
        String method = req.getParameter("method");
        //根据方法名调方法
        Class<? extends BaseServlet> clazz = this.getClass();
        try {
            Method declaredMethod = clazz.getDeclaredMethod(method, HttpServletRequest.class, HttpServletResponse.class);
            declaredMethod.setAccessible(true);
            declaredMethod.invoke(this, req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
